import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Random;

public class Juego extends Thread {

	protected Socket s;
	public DataInputStream i;
	protected DataOutputStream o;
	protected 	String Admitir = "Lo sentimos, el juego esta lleno";
	int jugadores;
	private 	int	numJugador=2; 
	int [] sumatoria=new int[5];
	int y=0;
	boolean ciclo=true;

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	public Juego (Socket s) throws IOException {
		this.s = s;
		i = new DataInputStream (new BufferedInputStream (s.getInputStream ()));
		o = new DataOutputStream (new BufferedOutputStream (s.getOutputStream ()));
	}

	protected static Vector handlers = new Vector ();

	/*public void run2 () {
		String name = s.getInetAddress ().getHostName();

		try {
			NotificarJugadores (name + " se ha unido al juego."); 
			if(handlers.size() < numJugador){
				AgregarJugador();
				NotificarJugadores (name + " se ha unido al juego."); 
				if(handlers.size() == numJugador){
					RepartirCartas();
					DarmeCartas();
					PreguntarCarta(); 
				}

			}

			else{
				AgregarJugador();
				NoAdmitir(Admitir);
			}
			while (true) {
		        String msg = i.readUTF ();
		        System.out.print(name + " - " + msg+"\n");
		 
		      }
		} catch (IOException ex) {
		      ex.printStackTrace ();
	    } finally {
			RemoverJugador();
			NotificarJugadores (name + " has left.");
			System.out.print(name + " has left." +"\n");
	      try {
	        s.close ();
	      } catch (IOException ex) {
	        ex.printStackTrace();
	      }
	    }
	}*/

	public void run () {
		String name = s.getInetAddress ().getHostName();
	    try {

			while (true) {  
				if(handlers.size() < numJugador){
					AgregarJugador();
					NotificarJugadores (name + " se ha unido al juego."); 
					if(handlers.size() == numJugador){
						RepartirCartas();
						DarmeCartas();
						PreguntarCarta(); 
						//NotificarJugadores ("pregunta");
						/*String msg = i.readUTF ();
				        NotificarJugadores (name + " - " + msg);
				        System.out.print(name + " - " + msg+"\n");*/
					}

				}	
				/*String msg = i.readUTF ();
		        NotificarJugadores (name + " - " + msg);
		        System.out.print(name + " - " + msg+"\n");*/
		 
		      }
			//NotificarJugadores ("pregunta");

	    } finally {
	    	RemoverJugador();
			NotificarJugadores (name + " has left.");
			System.out.print(name + " has left." +"\n");
	      try {
	        s.close ();
	      } catch (IOException ex) {
	        ex.printStackTrace();
	      }
	    }
	  }
	private void PreguntarCarta() {
		// TODO Auto-generated method stub
		boolean ciclo=true;
		String hit;
		synchronized (handlers) {
			Enumeration e = handlers.elements ();
			while (e.hasMoreElements ()) {
				Juego c = (Juego) e.nextElement ();
				try {
					synchronized (c.o) {
						while(ciclo){
							c.o.writeUTF("pregunta");
							c.o.flush();
								System.out.print("fsdfds \n");
								i.readUTF();
							//System.out.print(hit+"/n");
							//if(hit.equals("hitme"))
								//CartaExtra(c);
							//else
								ciclo=false;
						}
					}
					c.o.flush ();
				} catch (IOException ex) {
					ex.printStackTrace ();
					c.stop ();
				}
			}
		}
	}

	//@Override
	public void AgregarJugador() {
		// TODO Auto-generated method stub
		handlers.addElement (this);
	}

	//@Override
	public void CambiodeEstado() {
		// TODO Auto-generated method stub

	}

	//@Override
	public void Broadcast(String s) {
		// TODO Auto-generated method stub
		synchronized (handlers) {
			Enumeration e = handlers.elements ();
			while (e.hasMoreElements ()) {
				Juego c = (Juego) e.nextElement ();
				try {
					synchronized (c.o) {
						c.o.writeUTF (s);
					}
					c.o.flush ();
				} catch (IOException ex) {
					c.stop ();
				}
			}
		}
	}

	//@Override
	public void RemoverJugador() {
		// TODO Auto-generated method stub
		handlers.removeElement (this);
	}

	public void RepartirCartas() {
		Random rnd = new Random();
		String carta1 = null;
		String carta2=	null;
		synchronized (handlers) {
			Enumeration e = handlers.elements ();
			int x=0;
			while (e.hasMoreElements ()) {
				x++;
				carta1=String.valueOf(rnd.nextInt(51)+1);
				carta2=String.valueOf(rnd.nextInt(51)+1);
				Juego c = (Juego) e.nextElement ();
				try {
					synchronized (c.o) {
						c.o.writeUTF("carta");
						c.o.writeUTF(carta1);
						c.o.writeUTF("carta");
						c.o.writeUTF (carta2);
					}
					c.o.flush ();
				} catch (IOException ex) {
					c.stop ();
				}
				NotificarJugadores("Jugador"+x+": "+carta1);
			}
		}
		NotificarJugadores("Ahi tan sus cartas");
	}

	public void DarmeCartas() {
		Random rndServer = new Random();
		int [] arregloCartas = new int [5];
		arregloCartas[0]=rndServer.nextInt(51)+1;
		arregloCartas[1]=rndServer.nextInt(51)+1;
		for(int i=2;i<5;i++)
			arregloCartas[i]=0;

		NotificarJugadores("Dealer: "+String.valueOf(arregloCartas[0]));
	}

	public void CartaExtra(Juego c) {
		Random rnd = new Random();
		String cartaext = null;
		cartaext=String.valueOf(rnd.nextInt(51)+1);
		try {
			//c.o.writeUTF("carta");
			c.o.writeUTF(cartaext);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void DarmeCartaExtra() {

	}

	public void VoltearCartas() {

	}

	public void RepartirDinero() {

	}

	public void GanarPerder() {

	}

	public void LevantarConexion() {

	}

	public void NotificarSolo() {

	}

	public void NoAdmitir(String msg) {
		try {
			o.writeUTF(msg);
			RemoverJugador();
			//s.close ();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//@Override
	public void NotificarJugadores(String s) {
		synchronized (handlers) {
			Enumeration e = handlers.elements ();
			while (e.hasMoreElements ()) {
				Juego c = (Juego) e.nextElement ();
				try {
					synchronized (c.o) {
						c.o.writeUTF (s);
					}
					c.o.flush ();
				} catch (IOException ex) {
					c.stop ();
				}
			}
		}
	}
	
	public void leer(){  
						String hit;

							try {
								hit = i.readUTF();
								System.out.print(hit+"\n");//CartaExtra(c);
							} catch (IOException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							
						
		        //String msg = i.readUTF ();
		        //NotificarJugadores (name + " - " + msg);
		        //System.out.print(name + " - " + msg+"\n");
	}
}

